<template>
  <div class="root">
    <div class="divBox">
      <div class="content-container">
        <div class="container">
          <div style="" class="selectbox-root">
            <span class="selectbox-hint" style="min-width: 80px">核证标准</span>
            <div class="selectbox-deliver" />
            <el-cascader placeholder="全部" class="selectbox-input" @change="update" v-model="selectedCertification"
              :options="Certification" clearable>
            </el-cascader>
          </div>
          <div style="margin-left: 16px" class="selectbox-root">
            <span class="selectbox-hint" style="min-width: 60px">领域</span>
            <div class="selectbox-deliver" />
            <el-cascader placeholder="全部" class="selectbox-input" v-model="selectedArea" @change="update"
              :options="ProjectAreaDict" clearable>
            </el-cascader>
          </div>
          <div style="margin-left: 16px" class="selectbox-root">
            <span class="selectbox-hint" style="min-width: 60px">行业</span>
            <div class="selectbox-deliver" />
            <el-cascader placeholder="全部" @change="update" class="selectbox-input" v-model="selectedIndustry"
              :options="IndustryList" clearable>
            </el-cascader>
          </div>
          <div style="margin-left: 16px" class="selectbox-root">
            <span class="selectbox-hint" style="min-width: 60px">状态</span>
            <div class="selectbox-deliver" />
            <el-cascader placeholder="全部" @change="update" class="selectbox-input" v-model="selectedStatus"
              :options="statusList" clearable>
            </el-cascader>
          </div>
          <div style="margin-left: 16px" class="selectbox-root">
            <span class="selectbox-hint" style="min-width: 80px">立项日期</span>
            <div class="selectbox-deliver" />
            <el-date-picker v-model="selectStartDate" @change="update" type="datetime" placeholder="选择开始时间"
              align="right" size="medium">
            </el-date-picker>
            <el-date-picker v-model="selectEndDate" @change="update" type="datetime" placeholder="选择结束时间" align="right"
              size="medium">
            </el-date-picker>
          </div>
        </div>
        <div style="clear: both; height: 10px"></div>
        <div class="container" style="margin-top: 0">
          <div style="margin-right: 15px" class="selectbox-root">
            <span class="selectbox-hint" style="min-width: 120px">按项目搜索</span>
            <div class="selectbox-deliver" />
            <el-input class="selectbox-input" v-model="searchProjectName" clearable placeholder="输入名称"
              @clear="onClickSearch" @keyup.enter.native="onClickSearch" />
          </div>
          <button class="light-green-btn" @click="onClickSearch">查询</button>
          <div style="margin-right: 15px; margin-left: 15px" class="selectbox-root">
            <span class="selectbox-hint" style="min-width: 120px">按方法学搜索</span>
            <div class="selectbox-deliver" />
            <el-input class="selectbox-input" v-model="searchMethodName" placeholder="输入名称" @clear="onClickSearch"
              @keyup.enter.native="onClickSearch" clearable />
          </div>
          <button class="light-green-btn" @click="onClickSearch">查询</button>
        </div>
      </div>
      <div>
        <button style="margin-top: 0px; margin-bottom: 20px; width: 103px" class="normal-white-btn" @click="onclickAdd">
          +创建项目
        </button>
        <el-table :header-cell-style="{
          background: '#F2F5F7',
          border: '0px solid #DDDDDD',
          color: '#242B35',
          height: '64px',
        }" :show-header="true" :data="list" stripe :row-style="{ height: '64px' }" style="width: 100%">
          <el-table-column min-width="10"></el-table-column>
          <el-table-column label="序号" align="left" min-width="40">
            <template slot-scope="scope"><span>{{ getCurListNo(scope.$index) }}</span></template>
          </el-table-column>
          <el-table-column :show-overflow-tooltip="true" prop="projectName" label="项目名称" min-width="100" align="left" />
          <el-table-column align="left" prop="certificationCriteriaName" label="核证标准" min-width="80" />
          <el-table-column align="left" prop="fieldCodeName" label="领域" min-width="80" />
          <el-table-column align="left" prop="industryCodeName" label="行业" min-width="110" />
          <el-table-column align="left" prop="projectStatusName" label="状态" min-width="50" />
          <el-table-column align="left" prop="address" label="项目所在地" min-width="100" />
          <el-table-column align="left" prop="ownerName" label="业主名称" min-width="100" />
          <el-table-column align="left" prop="initiationDate" label="立项日期" min-width="80" />
          <el-table-column label="操作" min-width="220" align="center" fixed="right">
            <template slot-scope="scope">
              <div class="operation-buttons">
                <el-button 
                  size="mini" 
                  type="text" 
                  icon="el-icon-view" 
                  @click="toDetail(scope.row.id)"
                  class="btn-view"
                >
                  查看
                </el-button>
                <el-button 
                  size="mini" 
                  type="text" 
                  icon="el-icon-edit" 
                  @click="onEdit(scope.row.projectStatus, scope.row.id)"
                  :class="editStyleChange(scope.row.projectStatus)"
                >
                  修改
                </el-button>
                <el-button 
                  size="mini" 
                  type="text" 
                  icon="el-icon-delete" 
                  @click="onClickDelete(scope.row.projectStatus, scope.row.id)"
                  :class="publishStyleChange(scope.row.projectStatus)"
                >
                  删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div style="margin-top: 30px; margin-bottom: 10px" class="pageBox">
      <div style="flex-grow: 1" />
      <a style="margin: auto" class="pageBox-total-num">共{{ total }}条</a>
      <el-pagination style="margin: auto" background @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="current" :page-size="pageSize" :page-count="pageCount"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { getCarbonProjectPageList, deleteCarbonProject } from "@/api/carbonAssetApi";
import { getCertificationCriteriaDict } from "@/config/dictHelper";
import { getProjectAreaDict } from "@/config/dictHelper";
import { getBusinessDict } from "@/config/dictHelper";
import { getProjectStatusDict } from "@/config/dictHelper";
import { setListNo } from "@/libs/public";
export default {
  name: "projectDevHome",
  data() {
    return {
      searchProjectName: "",
      searchMethodName: "",
      selectedCertification: "", //被选中的标准
      selectedArea: "", //被选中的领域
      selectedIndustry: "", //被选中的行业
      selectedStatus: "", //被选中的状态
      selectStartDate: "",
      selectEndDate: "",
      list: [],
      total: 0,
      current: 1,
      pageCount: 1,
      pageSize: 10,
      Certification: [], //核证标准字典
      IndustryList: [], //行业字典
      ProjectAreaDict: [], //项目领域字典
      statusList: [], //项目状态字典,
    };
  },
  methods: {
    //判断是否发布，若发布了则修改样式
    editStyleChange(status) {
      if (status == "0100000001") {
        return "list-yello-text"
      } else {
        return "afterIssueEdit";
      }
    },
    publishStyleChange(status) {
      if (status == "0100000001") {
        return "list-red-text";
      } else {
        return "afterSubmitOffline";
      }
    },
    update() {
      this.getList(1);
    },
    onEdit(status, id) {
      if (status == "0100000001") {
        this.$router.push({
          path: "/carbonAssets/dev/projectEdit",
          query: { id: id, page: this.current },
        });
      } else {
        this.$message.warning("当前项目已立项，不可修改！");
      }
    },
    onclickAdd() {
      this.$router.push({ path: "/carbonAssets/dev/projectAdd" });
    },
    onClickDelete(status, id) {
      if (status[9] < 6) {
        this.$confirm("项目删除不可复原，请谨慎操作").then(() => {
          deleteCarbonProject(id).then(
            (res) => {
              this.$message.success("删除成功");
              this.getList(this.current);
            },
            (err) => {
              this.$message.error("删除失败");
            }
          );
        });
      } else {
        this.$message.warning("此项目已立项，不可删除");
      }
    },
    onClickSearch() {
      this.getList(1);
    },
    toDetail(id) {
      this.$router.push({
        path: "/carbonAssets/dev/projectDetail",
        query: { id: id },
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList(1);
    },
    handleCurrentChange(val) {
      this.current = val;
      this.getList(val);
    },
    getCurListNo(index) {
      let curNo = parseInt(index) + 1;
      let size = this.size || "10";
      let page = this.current - 1;
      let no = setListNo(page, size, curNo);
      return no ? no : 1;
    },
    getList(page) {
      const data = {
        asc: true,
        size: this.pageSize,
        name: this.searchProjectName,
        current: page,
        methodologyName: this.searchMethodName,
        certificationCriteria: this.selectedCertification
          ? this.selectedCertification[0]
          : "",
        fieldCode: this.selectedArea ? this.selectedArea[0] : "",
        industryCode: this.selectedIndustry ? this.selectedIndustry[0] : "",
        projectStatus: this.selectedStatus ? this.selectedStatus[0] : "",
        initiationDateStart: this.selectStartDate,
        initiationDateEnd: this.selectEndDate
      };
      getCarbonProjectPageList(data)
        .then((res) => {
          console.log('开发立项列表API响应:', res);
          if (res && res.records) {
            this.list = res.records;
            this.total = parseInt(res.total) || 0;
            this.current = parseInt(res.current) || 1;
            this.pageCount = Math.ceil(parseInt(res.total) / this.pageSize);
            this.list.map((v) => {
              for (var i in v) {
                v[i] = v[i] ? v[i] : "--";
                if (v[i] == " ") {
                  v[i] = "--";
                }
              }
            });
          }
        })
        .catch((errror) => {
          console.error('获取开发立项列表失败:', errror);
        });
    },
    // 格式化标准字典
    formatCertification(data) {
      data.map((v) => {
        let CertificationItem = {
          value: "",
          label: "",
        };
        if (v.name == "全部") {
          CertificationItem.label = v.name;
        } else {
          CertificationItem.value = v.value;
          CertificationItem.label = v.name;
        }
        this.Certification.push(CertificationItem);
      });
    },
    // 格式化行业字典
    formatIndustry(data) {
      data.map((v) => {
        let CertificationItem = {
          label: "",
        };
        if (v.name == "全部") {
          CertificationItem.label = v.name;
        } else {
          CertificationItem.value = v.value;
          CertificationItem.label = v.name;
        }
        this.IndustryList.push(CertificationItem);
      });
    },
    // 格式化领域字典
    formatArea(data) {
      data.map((v) => {
        let CertificationItem = {
          value: "",
          label: "",
        };
        if (v.name == "全部") {
          CertificationItem.label = v.name;
        } else {
          CertificationItem.value = v.value;
          CertificationItem.label = v.name;
        }
        this.ProjectAreaDict.push(CertificationItem);
      });
    },
    // 格式化状态字典
    formatStatus(data) {
      data.map((v) => {
        let CertificationItem = {
          value: "",
          label: "",
        };
        if (v.name == "全部") {
          CertificationItem.label = v.name;
        } else {
          CertificationItem.value = v.value;
          CertificationItem.label = v.name;
        }
        this.statusList.push(CertificationItem);
        this.statusList.splice(1, 0, this.statusList[this.statusList.length - 1])
      });
    },
  },
  mounted() {
    this.getList(1);
    this.formatCertification(getCertificationCriteriaDict(this.$store));
    this.formatIndustry(getBusinessDict(this.$store));
    this.formatArea(getProjectAreaDict(this.$store));
    this.formatStatus(getProjectStatusDict(this.$store));
  },
};
</script>
<style lang="scss" scoped>
.root {
  background: #f2f5f7;
}

.divBox {
  margin: 20px;
  background: #ffffff;
  box-shadow: 0px 2px 8px 0px #eaf0f3;
  border-radius: 8px;
}

.container {
  margin: 20px 0px 20px 0px;
  display: flex;
  flex-direction: row;
}

.content-container {
  display: flex;
  flex-direction: column;
  width: 100%;
}

>>>.el-cascader .el-input .el-input__inner,
>>>.el-cascader .el-input.is-focus .el-input__inner {
  border-color: transparent;
}

.selectbox-root {
  .el-date-picker {
    border: none;
  }
}

/* 操作按钮样式优化 */
.operation-buttons {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.operation-buttons .el-button {
  margin: 0;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.3s ease;
  font-size: 12px;
  min-width: 50px;
}

.operation-buttons .btn-view {
  color: #409EFF;
}

.operation-buttons .btn-view:hover {
  background-color: #ecf5ff;
  color: #409EFF;
}

.operation-buttons .btn-edit {
  color: #E6A23C;
}

.operation-buttons .btn-edit:hover {
  background-color: #fdf6ec;
  color: #E6A23C;
}

.operation-buttons .btn-delete {
  color: #F56C6C;
}

.operation-buttons .btn-delete:hover {
  background-color: #fef0f0;
  color: #F56C6C;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .operation-buttons {
    flex-direction: column;
    gap: 4px;
  }
  
  .operation-buttons .el-button {
    width: 100%;
    margin: 2px 0;
  }
}
</style>